package com.huika.renrench.control.investment.activity;

import java.util.HashMap;

import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response.Listener;
import com.google.gson.reflect.TypeToken;
import com.huika.lib.ui.widget.ToastMsg;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.view.TitleBarHelper;

/**
 * 
 * @description：预约投资
 * @author zc
 * @date 2015年6月4日 下午12:22:30
 */
public class BookInvestActivity extends BaseAct implements Listener<RequestResult<HashMap<String, String>>> {

	private TextView auto_invest_prompt;
	private ImageView auto_invest_icon;
	private ImageView book_state_icon;
	private Button confirm;
	private int flag = -1;//1为投资过来  2为投资管理过来
	private String userId;
	private TextView prompt;
	
	@Override
	public void setRootView() {
		setContentView(R.layout.activity_book_invest);
	}
	
	@Override
	protected void initData() {
		super.initData();
		Intent intent = getIntent();
		if(intent != null)
			flag = intent.getIntExtra("flag", -1);
		TitleBarHelper barHelper = new TitleBarHelper(this, -1);
		barHelper.setTitleMsg("预约投资");
		if(flag == 1) {
			barHelper.setLeftMsg("投资");
		} else {
			barHelper.setLeftMsg("返回");
		}
		userId = GlobalApp.userModel.getUserId();
	}
	
	private void getReserveInvest(String userId) {
		showLoadingDialog(getString(R.string.common_loading));
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putCommonTypeParam("userId", userId);
		FormResultRequest<HashMap<String, String>> request = new FormResultRequest<HashMap<String, String>>(UrlConstants.GETRESERVEINVEST, this, this, new TypeToken<RequestResult<HashMap<String, String>>>() {
		}.getType());
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}
	
	private void updateInvest(String userId, final int status) {
		showLoadingDialog(getString(R.string.common_loading));
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putCommonTypeParam("userId", userId);
		ajaxParams.putCommonTypeParam("status", status);
		FormResultRequest<String> request = new FormResultRequest<String>(UrlConstants.UPDATEINVEST, new Listener<RequestResult<String>>() {

			@Override
			public void onResponse(RequestResult<String> arg0) {
				dismissLoadingDialog();
				if(arg0.flag == 1) {
					reserveStatus = status;
					if(reserveStatus == 1) {
						book_state_icon.setImageResource(R.drawable.book_state_succes);
						confirm.setText("取消预约");
						auto_invest_prompt.setVisibility(View.VISIBLE);
						auto_invest_icon.setVisibility(View.VISIBLE);
					} else if(reserveStatus == 0) {
						aotuLoanStatus = 0;
						auto_invest_icon.setImageResource(R.drawable.auto_invest_normal);
						book_state_icon.setImageResource(R.drawable.book_state_cancel);
						confirm.setText("我要预约");
						auto_invest_prompt.setVisibility(View.INVISIBLE);
						auto_invest_icon.setVisibility(View.INVISIBLE);
					}
				}
			}
		}, this, new TypeToken<RequestResult<String>>() {
		}.getType());
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}
	
	private void updateAutoLoan(String userId, final int status) {
		showLoadingDialog(getString(R.string.common_loading));
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putCommonTypeParam("userId", userId);
		ajaxParams.putCommonTypeParam("status", status);
		FormResultRequest<String> request = new FormResultRequest<String>(UrlConstants.UPDATEAUTOLOAN, new Listener<RequestResult<String>>() {

			@Override
			public void onResponse(RequestResult<String> arg0) {
				dismissLoadingDialog();
				if(arg0.flag == 1) {
					aotuLoanStatus = status;
					if(aotuLoanStatus == 1) {
						auto_invest_icon.setImageResource(R.drawable.auto_invest_selected);
					} else if(aotuLoanStatus == 0) {
						auto_invest_icon.setImageResource(R.drawable.auto_invest_normal);
					}
				}
			} 
		}, this, new TypeToken<RequestResult<String>>() {
		}.getType());
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}
	
	@Override
	protected void initWidget() {
		super.initWidget();
		auto_invest_prompt = (TextView) findViewById(R.id.auto_invest_prompt);
		auto_invest_icon = (ImageView) findViewById(R.id.auto_invest_icon);
		book_state_icon = (ImageView) findViewById(R.id.book_state_icon);
		confirm = (Button) findViewById(R.id.confirm);
		prompt = (TextView) findViewById(R.id.prompt);
		
		confirm.setOnClickListener(this);
		auto_invest_icon.setOnClickListener(this);
		auto_invest_prompt.setOnClickListener(this);
		getReserveInvest(userId);
	}
	
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
			case R.id.confirm:
				if(reserveStatus == 1) {
//					book_state_icon.setImageResource(R.drawable.book_state_succes);
					updateInvest(userId, 0);
				} else if(reserveStatus == 0) {
					updateInvest(userId, 1);
//					book_state_icon.setImageResource(R.drawable.book_state_succes);
				}
				break;
			case R.id.auto_invest_prompt:
			case R.id.auto_invest_icon:
				if(aotuLoanStatus == 1) {
					updateAutoLoan(userId, 0);
				} else if(aotuLoanStatus == 0) {
					updateAutoLoan(userId, 1);
				} 
				break;

		}
	}
	
	private int reserveStatus = -1;//预约状态 0未成功1.已成功
	private int aotuLoanStatus = -1;//自动投资状态(针对预约状态为1的情况) 1自动 0非自动

	@Override
	public void onResponse(RequestResult<HashMap<String, String>> arg0) {
		dismissLoadingDialog();
		if(arg0.flag == 1) {
			reserveStatus = Integer.parseInt(arg0.getRs().get("reserveStatus"));
			aotuLoanStatus = Integer.parseInt(arg0.getRs().get("aotuLoanStatus"));
			if(reserveStatus == 1) {
				book_state_icon.setImageResource(R.drawable.book_state_succes);
				confirm.setText("取消预约");
				auto_invest_prompt.setVisibility(View.VISIBLE);
				auto_invest_icon.setVisibility(View.VISIBLE);
			} else if(reserveStatus == 0) {
				book_state_icon.setImageResource(R.drawable.book_state_cancel);
				confirm.setText("我要预约");
				auto_invest_prompt.setVisibility(View.INVISIBLE);
				auto_invest_icon.setVisibility(View.INVISIBLE);
			}
			if(aotuLoanStatus == 1) {
				auto_invest_icon.setImageResource(R.drawable.auto_invest_selected);
			} else if(aotuLoanStatus == 0) {
				auto_invest_icon.setImageResource(R.drawable.auto_invest_normal);
			}
			prompt.setText(Html.fromHtml(arg0.getRs().get("descript")));
		} else {
			
			ToastMsg.showToastMsg(this, arg0.msg);
		}
	}

}
